//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/13.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SnapKit
/*
'UICollectionView must be initialized with a non-nil layout parameter'
必须设置流水布局 否则就崩溃
*/
private let reuseIdentifier = "Cell"

//定义图片的数量
private let imageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    init() {
        
        let layout = UICollectionViewFlowLayout()
        //设置流水布局的参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        //设置分页属性
        collectionView?.pagingEnabled = true
        //去掉水平滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //注册单元格 这里如果想要注册 自定义的单元格 必须改成自定义的cell.self
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    //MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 4
    }
    
    /**
    *  cell长啥样
    */
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index  = indexPath.item
        return cell
    }
    
    //在滑动减速的时候执行动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / screenWidth)
        if page == imageCount - 1 {
            
            //执行动画
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
            }
            
        }
    }
    
}

class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
        didSet {
            startButton.hidden = true  
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //开始体验按钮的点击事件
    @objc func start() {
        
       //发出跳转控制器的通知
        NSNotificationCenter.defaultCenter().postNotificationName(appSwitchToViewController, object: nil)
    }
    
    //动画效果
    func startAnimation () {
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: { () -> Void in
            
            self.startButton.hidden = false
            
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) -> Void in
                
                print("完毕")
        }
        
        
        
    }
    
    private func setupUI() {
        //1.添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
    
        //设置约束
        //edges 表示相对于 四周约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        //3.设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        startButton.hidden = true

    }
    
    //设置点击事件

    
    //1.实例化子视图控件
    private lazy var iconImage: UIImageView = UIImageView()
    
    //2 点击体验按钮
    private lazy var startButton : UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
   

}

